/**
 * 签到页面
 * author:yinht
 * date:2017年6月2日19:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");

	var editmember = {
		userInfo: {},
		provinceList: {},
		editorItem: [],
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {

			//返回
			$("#return_sub").unbind().on("click", function() {
				window.localStorage.setItem("addedItem", "");
				window.localStorage.setItem("label","");
				window.history.back(-1);
			});
			$("#inc_items").unbind().on("click", function() {
				var param = newmenber.getParams();
				var userinfo = {
					"name": param.name,
					"sex": param.sex,
					"birthday": param.birthday,
					"tel": param.tel,
					"type": param.type,
					"address": param.address,

				}
				var userObjtamp = Object.assign(userinfo, param.national);
				console.log(userObj);
				var userObj = {
					status: "0",
					data: userObjtamp

				}
				var str = JSON.stringify(userObj);
				window.localStorage.setItem("userInfo", str);
				window.sessionStorage.setItem("pageTitle", $(".member-btn").html());
				window.location.href = "#/index/addmore";
			});
			$("#toewmenber").unbind().on("click", function() {
				
				newmenber.toSubmit();
			});
			//添加标签
			$(".tolable").unbind().on("click", function() {
				var param = newmenber.getParams();
				var userinfo = {
					"name": param.name,
					"sex": param.sex,
					"birthday": param.birthday,
					"tel": param.tel,
					"type": param.type,
					"address": param.address,

				}
				var userObjtamp = Object.assign(userinfo, param.national);
				console.log(userObj);
				var userObj = {
					status: "0",
					data: userObjtamp

				}
				var str = JSON.stringify(userObj);
				window.localStorage.setItem("userInfo", str);
				window.location.href = "#/index/addlabel";
			});

			$("#sex_select").unbind().on("click", function() {
				var picker = new mui.PopPicker();
				picker.setData([{
						value: '1',
						text: '男'
					},
					{
						value: '2',
						text: '女'
					}
				]);

				picker.show(function(selectItems) {
					$("#sex_select").val(selectItems[0].text);

					//					myProfile.changeUserInfo();
					picker.dispose();
				}, function(selectItems) {
					picker.dispose();
				});
			});
			$("#birth_select").unbind().on("click", function() {
				var dtPicker = new mui.DtPicker({
					"type": "date",
					"beginYear": 1949,
					"endYear": 2017
				});
				dtPicker.show(function(selectItems) {

					$("#birth_select").val(selectItems.y.text + "-" + selectItems.m.text + "-" + selectItems.d.text);

					dtPicker.dispose();
					//					myProfile.changeUserInfo();
				});
			});
			$("#level_select").unbind().on("click", function() {
				//初始化信息
				var initializestr = window.localStorage.getItem("initialize");
				var initializeObj = JSON.parse(initializestr);
				var picker = new mui.PopPicker();
				var levelArray = [];
				for(var i = 0; i < initializeObj.level.length; i++) {
					var item = {
						value: initializeObj.level[i].pk_defdoc,
						text: initializeObj.level[i].name
					}
					levelArray.push(item);
				}
				picker.setData(levelArray);

				picker.show(function(selectItems) {
					$("#level_select").val(selectItems[0].text);
					$("#level_select").attr("level", selectItems[0].value);

					//					myProfile.changeUserInfo();
					picker.dispose();
				}, function(selectItems) {
					picker.dispose();
				});
			});
			$("#dangci_select").unbind().on("click", function() {
				//初始化信息
				var initializestr = window.localStorage.getItem("initialize");
				var initializeObj = JSON.parse(initializestr);
				var picker = new mui.PopPicker();
				var dangciArray = [];
				for(var i = 0; i < initializeObj.grade.length; i++) {
					var item = {
						value: initializeObj.grade[i].pk_defdoc,
						text: initializeObj.grade[i].name
					}
					dangciArray.push(item);
				}
				picker.setData(dangciArray);

				picker.show(function(selectItems) {
					$("#dangci_select").val(selectItems[0].text);
					$("#dangci_select").attr("Consumptionlevel", selectItems[0].value);

					//					myProfile.changeUserInfo();
					picker.dispose();
				}, function(selectItems) {
					picker.dispose();
				});
			});
			$("#mygood_select").unbind().on("click", function() {
				//初始化信息
				var initializestr = window.localStorage.getItem("initialize");
				var initializeObj = JSON.parse(initializestr);
				var picker = new mui.PopPicker();
				var mygoodsArray = [];
				for(var i = 0; i < initializeObj.ownSeries.length; i++) {
					var item = {
						value: initializeObj.ownSeries[i].code,
						text: initializeObj.ownSeries[i].name
					}
					mygoodsArray.push(item);
				}
				picker.setData(mygoodsArray);

				picker.show(function(selectItems) {
					$("#mygood_select").val(selectItems[0].text);
					//					$("#mygood_select").attr("mygood",selectItems[0].value);
					picker.dispose();
				}, function(selectItems) {
					picker.dispose();
				});
			});
			$("#system_select").unbind().on("click", function() {
				//初始化信息
				var initializestr = window.localStorage.getItem("initialize");
				var initializeObj = JSON.parse(initializestr);
				var picker = new mui.PopPicker();
				var systemArray = [];
				for(var i = 0; i < initializeObj.system.length; i++) {
					var item = {
						value: initializeObj.system[i].pk_defdoc,
						text: initializeObj.system[i].name
					}
					systemArray.push(item);
				}

				picker.setData(systemArray);

				picker.show(function(selectItems) {
					$("#system_select").val(selectItems[0].text);
					$("#system_select").attr("Subordinate", selectItems[0].value);
					//						console.log(selectItems[0].value);
					picker.dispose();
				}, function(selectItems) {
					picker.dispose();
				});
			});
			$("#address_select").unbind().on("click", function() {

				var cityPicker3 = new mui.PopPicker({
					layer: 3,
					changeFn: function(even) {
						var nextPickerElement = this.nextSibling;
						if(nextPickerElement && nextPickerElement.picker) {
							var eventData = event.detail || {};
							var preItem = eventData.item || {};
							if(preItem.value) {
								var pk_user = window.localStorage.getItem("pk_user");
								var param = {
									"pk_user": pk_user,
									"pid": preItem.value
								}
								common.loadData({
									url: "/service/RRTPubServlet?method=getCityList",
									param: param,
									successCallBack: function(json) {
										console.log("调用后台ajax返回值为：" + JSON.stringify(json));
										if(json.status == "0") {
											nextPickerElement.picker.setItems(json.data);
										} else {
											common.showTip({
												content: json.message
											});
										}
									}, //回调函数
									dataSource: "getCityList"
								});
							} else {
								nextPickerElement.picker.setItems([]);
							}
						}
					}
				});
				cityPicker3.setData(newmenber.provinceList);
				cityPicker3.show(function(selectItems) {

					$("#province_address").text(selectItems[0].text);
					console.log(selectItems[0].text);
					$("#province_address").attr("receiverProvinceId", selectItems[0].value);
					//console.log( selectItems[0].value);
					$("#city_address").text(selectItems[1].text);
					//console.log(selectItems[1].text);
					$("#city_address").attr("receiverCityId", selectItems[1].value);
					//console.log(selectItems[1].value);
					$("#region_address").text(selectItems[2].text);
					//console.log(selectItems[2].text);
					$("#region_address").attr("receiverRegionId", selectItems[2].value);
					//console.log(selectItems[2].value);
					$("#address_select").val(selectItems[0].text + selectItems[1].text + selectItems[2].text);
					//					console.log(selectItems[0].text+selectItems[1].text+selectItems[2].text);
					$("#address_select").attr("receiverCity", selectItems[0].text);
					$("#address_select").attr("receiverCityId", selectItems[0].value);
					$("#address_select").attr("receiverProvince", selectItems[1].text);
					$("#address_select").attr("receiverProvinceId", selectItems[1].value);
					$("#address_select").attr("receiverRegion", selectItems[2].text);
					$("#address_select").attr("receiverRegionId", selectItems[2].value);
					cityPicker3.dispose();
				}, function() {
					cityPicker3.dispose();
				});

			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: true,
					from: "left"
				});
			});
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var from = "right"; //从右边过来
				if((page_name == "men_details") || (page_name == "Management") || (page_name == "memberShip")) {
					from = "left";
				}
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: from
				});
			});
			Path.listen();
		},
		getParams: function() {
			//初始化信息
			var initializestr = window.localStorage.getItem("initialize");
			var initializeObj = JSON.parse(initializestr);
			console.log(initializeObj);
			var pk_user = window.localStorage.getItem("pk_user");
			var name = $("#username").val();
			var sex = $("#sex_select").val();
			var birthday = $("#birth_select").val();
			var tel = $("#tel").val();
			var receiverAddr = $("#detail_address").val();
			//national中的数组
			var national = $("#national").val();
			var Ancestral = $("#oldhome").val();
			var hobbies = $("#hobby").val();
			var entityname = $("#companyname").val();

			//
			var Subordinate = $("#system_select").val();
			var position = $("#jobs").val();
			//客户标签未解决

			//			var label = window.localStorage.getItem("label");
			var label = [];
			var consumption = $("#money_select").val();
			var level = $("#level_select").val();
			var Consumptionlevel = $("#dangci_select").val();
			var mygoodsitems = $("#mygooditem").val();
			var mygood = $("#mygoods").val();
			var competinggooditems = $("#competingitems").val();
			var competinggood = $("#competinggoods").val();
			var docker = initializeObj.userInfo.pk_psndoc;
			var dockertel = initializeObj.userInfo.phone;

			//自由参数
//			var Subordinate = $("#system_select").attr("Subordinate");
//			var Consumptionlevel = $("#dangci_select").attr("Consumptionlevel");
//			var level = $("#level_select").attr("level");
			var receiverCity = $("#address_select");
			console.log(receiverCity);
			var receiverCityId = $("#address_select").attr("receiverCityId");
			var receiverProvince = $("#address_select").attr("receiverProvince");
			var receiverProvinceId = $("#address_select").attr("receiverProvinceId");
			var receiverRegion = $("#address_select").attr("receiverRegion");
			var receiverRegionId = $("#address_select").attr("receiverRegionId");
			var national = {
				"national": national, //民族
				"Ancestral": Ancestral, //祖籍
				"hobbies": hobbies, //爱好
				"entityname": entityname, //单位名称
				"position": position, //职务
				"subordinate": Subordinate, //所属系统
				"memolabel": label, //客户标签
				"consumption": consumption, //消费金额
				"level": level, //等级
				"consumptionlevel": Consumptionlevel, //消费等级
				"mygoodsitems": mygoodsitems, //我品系列
				"mygood": mygood, //我品产品
				"competinggooditems": competinggooditems, //竞品品类
				"competinggood": competinggood, //竞品产品
				"docker": docker, //跟进人
				"dockertel": dockertel //跟进人电话
			};
			var address = {
				"receiverAddr": receiverAddr,
				"receiverCity": receiverCity,
				"receiverRegion": receiverRegion,
				"receiverProvince": receiverProvince,
				"receiverCityId": receiverCityId,
				"receiverProvinceId": receiverProvinceId,
				"receiverRegionId": receiverRegionId
			};
			var type;
			var update = window.localStorage.getItem("update");
			if(update == "新增") {
				type = "0";
			} else if(update == "变更") {
				type = "1";
			}
			var param = {
				"pk_user": pk_user,
				"name": name,
				"sex": sex,
				"birthday": birthday,
				"tel": tel,
				"type": type,
				"address": address,
				"national": national
			}
			return param;
		},
		//提交新增信息
		toSubmit: function() {

			var param = newmenber.getParams();
			common.loadData({
				url: "/service/RRTPubServlet?method=editorMessage",
				param: param,
				successCallBack: function(json) {
					console.log("调用后台ajax返回值为：" + JSON.stringify(json));
					if(json.status == "0") {
						//newmenber.provinceList = json.data;
						common.showTip({
							time: 3000,
							content: "提交成功！",
							callback: function() {
								window.history.back(-1);
								//common.showTip({content:"我已经关闭"});
							}
						});
						return;

					} else {
						common.showTip({
							content: json.message
						});
					}
				}, //回调函数
			});
		},
		/**
		 * 页面数据加载
		 */
		loadData: function() {
			var userstr = window.localStorage.getItem("userInfo");
			var userObj = JSON.parse(userstr);
			//初始化信息
			var initializestr = window.localStorage.getItem("initialize");
			var initializeObj = JSON.parse(initializestr);
			var docker = initializeObj.userInfo.psnName;
			var dockertel = initializeObj.userInfo.phone;
			$("#djname").val(docker);
			$("#phone").val(dockertel);
			//把userObj 填上
			//获取本地的type
			var type = window.localStorage.getItem("type");
			//编辑
//			var bianji = window.sessionStorage.getItem("bianji");
//			if(bianji == "0") {
				$(".air_title").text("编辑会员");
				var addinput = "";
				var memohtml = "";
				var rp = "";
				var rc = "";
				$.each(userObj.data, function(key, val) {
					if(val != "") {
						$("[itemkey=" + key + "]").parent().removeClass("hidden");
						$("[itemkey=" + key + "]").val(val);
						if(key == "address") {
							var address = val;
							$.each(address, function(key, val) {
								$("[itemkey=" + key + "]").parent().removeClass("hidden");
								$("[itemkey=" + key + "]").val(val);
								if(key == "receiverProvince" && rc != val) {
									addinput += val;
									rp = val;
								}
								if(key == "receiverCity" && rp != val) {
									addinput += val;
									rc = val;
								}
								if(key == "receiverRegion") {
									addinput += val;
								}
							});
						}
						if(key == "memolabel") {
							if(val.length > 0) {
								var memolabel = val;
								$.each(memolabel, function(index, val) {
									var memo = val;
									$.each(memo, function(key, val) {
										$("[itemkey=" + key + "]").parent().removeClass("hidden");
										if(key == "memo") {
											memohtml += val;
										}
									})
								});
							}
						}
					}
				});
				$("[itemkey=receiverProvince]").val(addinput);
				$("[itemkey=memolabel]").val(memohtml);
//			}
//			window.sessionStorage.removeItem("bianji");
			var pk_user = window.localStorage.getItem("pk_user");
			var param = {
				"pk_user": pk_user,
			}
			common.loadData({
				url: "/service/RRTPubServlet?method=getProvinceList",
				param: param,
				successCallBack: function(json) {
					console.log("调用后台ajax返回值为：" + JSON.stringify(json));
					if(json.status == "0") {
						newmenber.provinceList = json.data;

					} else {
						common.showTip({
							content: json.message
						});
					}
				}, //回调函数
				dataSource: "getCityList"
			});
			var iscroll = require("iscroll");
			common.initIscroll({
				container: "iscrollContent_menber",
				isVertical: true,
				showScrollBar: true
			});

			var pageTitle = window.sessionStorage.getItem("pageTitle");
			//			var newmemberDetail = window.localStorage.getItem("newmemberDetail");
			//			var mdkey = "";
			//			var mdobj = {};
			//			if(newmemberDetail !== null) {
			//				$.each(JSON.parse(newmemberDetail), function(index, val) {
			//					var item = val;
			//					$.each(item, function(key, val) {
			//						if(key == "key") {
			//							mdkey = val;
			//						}
			//						if(key == "val") {
			//							mdobj[mdkey] = val;
			//						}
			//					});
			//				});
			//				console.log(mdobj);
			//				$.each(mdobj, function(key, val) {
			//					if(val != "") {
			//						$("[itemkey=" + key + "]").parent().removeClass("hidden");
			//						$("[itemkey=" + key + "]").val(val);
			//					}
			//				});
			//			}
			if(pageTitle !== null) {
				$(".air_title").html(pageTitle);
			}

			var addedItem = window.localStorage.getItem("addedItem");
			if(addedItem != null && addedItem != "") {
				$.each(JSON.parse(addedItem), function(index, val) {
					var additem = val;
					$.each(additem, function(key, val) {
						if(key == "key") {
							$("[itemkey=" + val + "]").parent().removeClass("hidden");
						}
					})
				});
			}
			
			var label =	window.localStorage.getItem("label");
			if(label !=null && label!=""){
				var array= JSON.parse(label);
				console.log(array);
				var html="";
				for(var i = 0;i<array.length;i++){
					html += '<span>'+array[i]+'</span>';
				}
					$("#hobby").html();
			}
			//			window.localStorage.removeItem("newmemberDetail");
			window.sessionStorage.removeItem("pageTitle");
		},
		/**
		 * each page init mothed
		 */
		initialize: function() {

			this.loadData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = editmember;
});